package com.jt.interceptor;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.Connection;
import java.util.Properties;

@Intercepts(
        @Signature(
                type=StatementHandler.class,
                method = "prepare",
                args={Connection.class,Integer.class}

))
public class ShowSqlInterceptor2 implements Interceptor {

    private boolean showSql=false;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {


        if(showSql) {
            System.out.println("打印sql语句");
            StatementHandler statementHandler = (StatementHandler) invocation.getTarget();

            BoundSql boundSql = statementHandler.getBoundSql();
            System.out.println(boundSql.getSql());
        }
        return invocation.proceed();
    }


    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {

        showSql = Boolean.parseBoolean(properties.getProperty("showSql"));
        System.out.println("-----------"+showSql);
    }
}
